﻿// // --------------------------------------------------------------------------------------------------------------------
// // <copyright file="ConfigureTermsAndConditionsPage.cs" company="Collaboris">
// //   Copyright Collaboris
// // </copyright>
// // <summary>
// //   
// // </summary>
// // --------------------------------------------------------------------------------------------------------------------

namespace DocRead.Selenium.Tests.DocRead.PageObjects.CA
{
    #region

    using Collaboris.ShareTest.Core;
    using Collaboris.ShareTest.SharePoint2010.PageObjects;

    using OpenQA.Selenium.Remote;

    using global::DocRead.Selenium.Tests.DocRead.ControlObjects;

    #endregion

    /// <summary>
    /// 	Maps to the DocRead Configure Terms and Conditions Page located in Central Admin
    /// </summary>
    public class ConfigureTermsAndConditionsPage : SPTPageObjectBase
    {
        #region Constants and Fields

        private TermsAndConditionsForm form;

        #endregion

        #region Constructors and Destructors

        public ConfigureTermsAndConditionsPage(RemoteWebDriver driver, string fullUrl)
            : base(driver, fullUrl)
        {
        }

        public ConfigureTermsAndConditionsPage(RemoteWebDriver driver, string fullUrl, string verifyElementOnLoad)
            : base(driver, fullUrl, verifyElementOnLoad)
        {
        }

        public ConfigureTermsAndConditionsPage(
            RemoteWebDriver driver, string fullUrl, string verifyElementOnLoad, string loginFileName)
            : base(driver, fullUrl, verifyElementOnLoad, loginFileName)
        {
        }

        #endregion

        #region Public Properties

        public TermsAndConditionsForm Form
        {
            get
            {
                if (this.form == null)
                {
                    this.form = new TermsAndConditionsForm(this.webDriver);
                    this.RefreshPage();
                }

                return this.form;
            }
            set
            {
                this.form = value;
            }
        }

        #endregion

        #region Public Methods

        public void Save()
        {
            this.webDriver.SendKeysAndClear(this.Form.MainStatementElement, this.Form.MainStatement);

            this.RefreshIfNeeded();
        }

        #endregion

        #region Methods

        /// <summary>
        /// 	Only Refreshe if the url changes
        /// </summary>
        private void RefreshIfNeeded()
        {
            string previousUrl = this.webDriver.Url;
            this.Form.OkButton.Click();
            if (this.webDriver.Url == previousUrl)
            {
                this.Form.RefreshForm();
            }
        }

        public override void RefreshPage()
        {
            base.RefreshPage();

            this.Form = null;
        }


        #endregion
    }
}